{% extends 'app01/admin/home.html' %}

{% block content %}
    <div class="panel panel-default">
        <div class="panel-heading">资源管理</div>
        <div class="panel-body">
            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th>资源名称</th>
                    <th>资源描述</th>
                    <th>上传者</th>
                    <th>删除操作</th>
                    <th>修改等级</th>
                </tr>
                </thead>

           <tbody>


            {% for file in c_page %}
              <tr>
              <td>{{ file.name }}</td>
              <td>{{ file.descn }}</td>
                  <td>{{ file.uploader.username }}</td>
              <td><a href="#" class="del_btn" del_id="{{ file.pk }}">删除</a></td>
              <td><a href="#" class="change_level" source_id="{{ file.pk }}">{{ file.get_level_display }}(点击修改)</a></td>
              </tr>
            {% endfor %}
             </tbody>
     </table>
        </div>
    </div>
    {# 分页显示 #}
    <div class="pager">
        <nav aria-label="Page navigation">
            <ul class="pagination">
                {{ page_html }}
            </ul>
        </nav>
    </div>

{% endblock %}

{% block js %}
    <script>
    //修改资源等级
    $('.change_level').click(function () {
        //拿到要修改的资源的id
        let source_id = $(this).attr('source_id')
        let level = prompt('修改等级有：初级(3)、中级(2)、高级(1)，请输入对应数字。')
        if(parseInt(level)){
            level = parseInt(level)
            if(level>0 && level <4){
               $.ajax({
                url:'',
                type:'post',
                data:{'level':level,'source_id':source_id},
                success:function (attr) {
                    if(attr.code===100){
                        alert(attr.msg)
                        window.location.reload()
                    }
                    else {
                        alert(attr.msg)
                    }
                }
            })
            }
            else {
                alert('输入对应的等级：1，2，3.')
            }


        }
        else {
            alert('请输入等级对应的数字，1，2，3.')
        }
    })


        $('.del_btn').click(function () {
            //删除的二次确认
            let op = confirm('确认删除吗？')
            let del_id=$(this).attr('del_id')
              $.ajax({
            url:'',
            type:'post',
            data:{'del_id':del_id,'op':op},
            success:function (array) {
                if (array.code===100){
                    alert(array.msg)
                    window.location.reload()
                }
                else if( array.code===200 && op===true){
                    alert(array.msg)
                }

            }
        })
        })

//鼠标获取焦点时，错误信息不显示了。
        $('#id_confirm_pwd').focus(function () {
            $('#error').text('')

        })


    //修改密码的ajax请求

    $('#id_editer').click(function () {

        let old_pwd = $('#id_old_pwd').val()
        let new_pwd = $('#id_new_pwd').val()
        let comfir_pwd = $('#id_confirm_pwd').val()

        $.ajax({
            url:'/app01/set_password/',
            type: 'post',
            data:{'old_pwd':old_pwd,'new_pwd':new_pwd,'confirm_pwd':comfir_pwd},
            success:function (attr) {
                if(attr.code===1000){
                    alert(attr.msg)
                    window.location.reload()
                } else if(attr.code===2000){
                    $('#error').text(attr.msg)
                }else  if(attr.code===3000){
                    $('#error').text(attr.msg)
                }
            }
        })


    })


    </script>
{% endblock %}